/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Controls;
using MyPhotoIndex.Plugins;
using MyPhotoIndex.Interfaces;
using System.Drawing.Drawing2D;

namespace MyPhotoIndex.Controls
{
    public partial class FormTemplate : FadingForm
    {
        private bool m_enableDefaultOk = true;
        private bool m_enableDefaultCancel = true;
        private bool m_okVisable = true;
        private bool m_cancelVisable = true;
        private bool m_saveVisable = false;

        public FormTemplate()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;

            panel1.Paint += new PaintEventHandler(panel1_Paint);

            okButton.Click += new EventHandler(OkButton_Click);
            cancelButton.Click += new EventHandler(CancelButton_Click);
            saveButton.Click += new EventHandler(SaveToolStripButton_Click);

            this.Load += new EventHandler(FormTemplate_Load);
            this.ClientSizeChanged += new EventHandler(FormTemplate_ClientSizeChanged);

            saveButton.Visible = false;
        }

        void FormTemplate_ClientSizeChanged(object sender, EventArgs e)
        {
            //if (this.Controls.Count == 2)
            //{
            //    Control control = this.Controls[0];
            //    if (control != panel1)
            //    {
            //        control.Dock = DockStyle.Fill;

            //        //control.Height = this.ClientSize.Height - panel1.Height;

            //        //control.Top = 0;
            //        //control.Left = 0;
            //        //control.Width = this.ClientSize.Width;
            //    }
            //}
        }

        void FormTemplate_Load(object sender, EventArgs e)
        {
            FormTemplate_ClientSizeChanged(this, EventArgs.Empty);
            cancelButton.Visible = m_cancelVisable;
            okButton.Visible = m_okVisable;
            saveButton.Visible = m_saveVisable;

            if (okButton.Visible == false &&
                cancelButton.Visible == true)
            {
                cancelButton.Bounds = okButton.Bounds;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                if (m_enableDefaultOk == true)
                {
                    OkButton_Click(this, EventArgs.Empty);
                }
            }
            else
                if (keyData == Keys.Escape)
                {
                    if (m_enableDefaultCancel == true)
                    {
                        CancelButton_Click(this, EventArgs.Empty);
                    }
                }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        [Description("Save button visable property")]
        [Category("FormTemplate")]
        public bool SaveButtonVisable
        {
            get
            {
                return m_saveVisable;
            }
            set
            {
                m_saveVisable = value;
            }
        }

        [Description("Enable the Save button")]
        [Category("FormTemplate")]
        public bool SaveButtonEnabled
        {
            get
            {
                return saveButton.Enabled;
            }
            set
            {
                saveButton.Enabled = value;
            }
        }

        [Description("Enable default Ok button")]
        [Category("FormTemplate")]
        public bool EnableDefaultOk
        {
            get
            {
                return m_enableDefaultOk;
            }
            set
            {
                m_enableDefaultOk = value;
            }
        }

        [Description("Enable default cancel button")]
        [Category("FormTemplate")]
        public bool EnableDefaultCancel
        {
            get
            {
                return m_enableDefaultCancel;
            }
            set
            {
                m_enableDefaultCancel = value;
            }
        }

        [Description("Enable the ok button")]
        [Category("FormTemplate")]
        public bool OkButtonEnabled
        {
            get
            {
                return okButton.Enabled;
            }
            set
            {
                okButton.Enabled = value;
            }
        }

        [Description("Ok button visable property")]
        [Category("FormTemplate")]
        public bool OkButtonVisable
        {
            get
            {
                return m_okVisable;
            }
            set
            {
                m_okVisable = value;
            }
        }

        [Description("Enable the cancel button")]
        [Category("FormTemplate")]
        public bool CancelButtonEnabled
        {
            get
            {
                return cancelButton.Enabled;
            }
            set
            {
                cancelButton.Enabled = value;
            }
        }

        [Description("Cancel button visable property")]
        [Category("FormTemplate")]
        public bool CancelButtonVisable
        {
            get
            {
                return m_cancelVisable;
            }
            set
            {
                m_cancelVisable = value;
            }
        }

        [Description("Cancel button ToolTipText property")]
        [Category("FormTemplate")]
        [Localizable(true)]
        public String CancelButtonToolTipText
        {
            get
            {
                return cancelButton.Text;
            }
            set
            {
                cancelButton.Text = value;
            }
        }

        [Description("Ok button ToolTipText property")]
        [Category("FormTemplate")]
        [Localizable(true)]
        public String OkButtonToolTipText
        {
            get
            {
                return okButton.Text;
            }
            set
            {
                okButton.Text = value;
            }
        }

        protected virtual void OkButton_Click(object sender, EventArgs e)
        {
            if (okButton.Enabled == false)
            {
                return;
            }

            DialogResult = GetDialogResult(true);
            this.Close();
        }

        protected virtual void CancelButton_Click(object sender, EventArgs e)
        {
            if (cancelButton.Enabled == false)
            {
                return;
            }

            DialogResult = GetDialogResult(false);
            this.Close();
        }

        protected virtual void SaveToolStripButton_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual DialogResult GetDialogResult(bool positive)
        {
            if (positive == true)
            {
                return DialogResult.OK;
            }
            else
            {
                return DialogResult.Cancel;
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            pevent.Graphics.Clear(Color.White);
        }

        void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;

            Rectangle gradDrawingRect = panel1.ClientRectangle;// e.ClipRectangle;

            gradDrawingRect.Height -= 2;

            if (gradDrawingRect.Height <= 0 ||
                gradDrawingRect.Width <= 0)
            {
                return;
            }

            using (Brush parentBrush = new LinearGradientBrush(gradDrawingRect,
            Color.FromArgb(222, 236, 253), Color.FromArgb(171, 191, 216), LinearGradientMode.Vertical))
            {
                graphics.FillRectangle(parentBrush, gradDrawingRect);
            }

            using (Pen pen = new Pen(Color.FromArgb(108, 131, 158)))
            {
                Point start = new Point(0, panel1.ClientRectangle.Height - 2);
                Point end = new Point(panel1.ClientRectangle.Width - 1, panel1.ClientRectangle.Height - 2);
                graphics.DrawLine(pen, start, end);
            }

            using (Brush gradBrush = new LinearGradientBrush(gradDrawingRect,
                Color.FromArgb(222, 236, 253), Color.FromArgb(108, 131, 158), LinearGradientMode.Vertical))
            {
                using (Pen pen = new Pen(gradBrush))
                {
                    Point start = new Point(0, 0);
                    Point end = new Point(0, panel1.ClientRectangle.Height - 1);
                    graphics.DrawLine(pen, start, end);

                    start = new Point(panel1.ClientRectangle.Width - 2, 0);
                    end = new Point(panel1.ClientRectangle.Width - 2, panel1.ClientRectangle.Height - 1);
                    graphics.DrawLine(pen, start, end);
                }
            }

            using (Pen pen = new Pen(Color.FromArgb(79, 94, 113)))
            {
                Point start = new Point(0, panel1.ClientRectangle.Height - 1);
                Point end = new Point(panel1.ClientRectangle.Width, panel1.ClientRectangle.Height - 1);
                graphics.DrawLine(pen, start, end);

                start = new Point(panel1.ClientRectangle.Width - 1, 0);
                end = new Point(panel1.ClientRectangle.Width - 1, panel1.ClientRectangle.Height - 1);
                graphics.DrawLine(pen, start, end);
            }
        }
    }
}